package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.domain.ZhwyChargeitem;
import com.ruoyi.domain.ZhwyPaymentmethod;
import com.ruoyi.mapper.ZhwyChargeitemMapper;
import com.ruoyi.mapper.ZhwyPaymentmethodMapper;
import com.ruoyi.system.domain.ZhwyBuilding;
import com.ruoyi.system.domain.ZhwyShops;
import com.ruoyi.system.mapper.ZhwyBuildingMapper;
import com.ruoyi.system.mapper.ZhwyShopsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ZhwyChargedataMapper;
import com.ruoyi.domain.ZhwyChargedata;
import com.ruoyi.service.IZhwyChargedataService;

/**
 * 收费数据Service业务层处理
 * 
 * @author ningning
 * @date 2024-05-07
 */
@Service
public class ZhwyChargedataServiceImpl implements IZhwyChargedataService 
{
    @Autowired
    private ZhwyChargedataMapper zhwyChargedataMapper;
    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;
    @Autowired
    private ZhwyChargeitemMapper zhwyChargeitemMapper;
    @Autowired
    private ZhwyPaymentmethodMapper zhwyPaymentmethodMapper;
    @Autowired
    private ZhwyBuildingMapper zhwyBuildingMapper;

    @Override
    public List<ZhwyChargedata> selectZhwyChargedataByCode(String code) {


        ZhwyShops zhwyShops = new ZhwyShops();
        List<ZhwyChargedata> list = new ArrayList<>();
        zhwyShops.setCode(code);
        List<ZhwyShops> shops = zhwyShopsMapper.selectZhwyShopsList(zhwyShops);
        for (ZhwyShops sh : shops){
            ZhwyChargedata zhwyChargedata = new ZhwyChargedata();
            zhwyChargedata.setShopsId(sh.getId());
            List<ZhwyChargedata> list1 = zhwyChargedataMapper.selectZhwyChargedataList(zhwyChargedata);
            list.addAll(list1);
        }
        for(ZhwyChargedata zcd : list){
            ZhwyShops zhwyShops11 = zhwyShopsMapper.selectZhwyShopsById(zcd.getShopsId());
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(zcd.getChargeItemId());
            ZhwyPaymentmethod zhwyPaymentmethod = zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zcd.getPayId());
            zcd.setShopsCode(zhwyShops11.getCode());
            if (zhwyChargeitem != null) {
                zcd.setChargeItemName(zhwyChargeitem.getName());
            }
            if (zhwyPaymentmethod != null){
                zcd.setPaymentMethodName(zhwyPaymentmethod.getName());
            }
        }
        return list;
    }

    @Override
    public List<ZhwyChargedata> findByBuildingId(Long id) {
        List<ZhwyChargedata> list = new ArrayList<>();
        ZhwyShops zhwyShops = new ZhwyShops();
        zhwyShops.setbId(id);
        List<ZhwyShops> shops = zhwyShopsMapper.selectZhwyShopsList(zhwyShops);
        for (ZhwyShops zhwyShop: shops){
            ZhwyChargedata zhwyChargedata = new ZhwyChargedata();
            zhwyChargedata.setShopsId(zhwyShop.getId());
            List<ZhwyChargedata> list1 = zhwyChargedataMapper.selectZhwyChargedataList(zhwyChargedata);
            if (list1.size()>0){
                list.addAll(list1);
            }
        }
        for (ZhwyChargedata zdt: list){
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(zdt.getChargeItemId());
            ZhwyShops zhwyShops2 = zhwyShopsMapper.selectZhwyShopsById(zdt.getShopsId());
            zdt.setChargeItemName(zhwyChargeitem.getName());
            zdt.setShopsCode(zhwyShops2.getCode());
        }
        return list;
    }

    @Override
    public List<ZhwyChargedata> findByDTId(Long id) {
        List<ZhwyChargedata> list = new ArrayList<>();
        ZhwyBuilding zhwyBuilding = new ZhwyBuilding();
        zhwyBuilding.setdId(id);
        List<ZhwyBuilding> zhwyBuildings = zhwyBuildingMapper.selectZhwyBuildingList(zhwyBuilding);
        if (zhwyBuildings.size()>0){

            for (ZhwyBuilding zb: zhwyBuildings){
                List<ZhwyChargedata> byBuildingId = this.findByBuildingId(zb.getId());
                if (byBuildingId.size()>0){
                    list.addAll(byBuildingId);
                }
            }
        }

        return list;
    }

    /**
     * 查询收费数据
     * 
     * @param id 收费数据主键
     * @return 收费数据
     */
    @Override
    public ZhwyChargedata selectZhwyChargedataById(Long id)
    {
        return zhwyChargedataMapper.selectZhwyChargedataById(id);
    }

    /**
     * 查询收费数据列表
     * 
     * @param zhwyChargedata 收费数据
     * @return 收费数据
     */
    @Override
    public List<ZhwyChargedata> selectZhwyChargedataList(ZhwyChargedata zhwyChargedata)
    {
//        System.out.println(zhwyChargedata);
        if (zhwyChargedata.getShopsCode()!=null){
            ZhwyShops zhwyShops1=new ZhwyShops();
            zhwyShops1.setCode(zhwyChargedata.getShopsCode());
            List<ZhwyShops> zhwyShopsList = zhwyShopsMapper.selectZhwyShopsList(zhwyShops1);
            ZhwyShops zhwyShops2 = zhwyShopsList.get(0);
            zhwyChargedata.setShopsId(zhwyShops2.getId());
        }
        List<ZhwyChargedata> list = zhwyChargedataMapper.selectZhwyChargedataList(zhwyChargedata);
        for(ZhwyChargedata zcd : list){
            ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(zcd.getShopsId());
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(zcd.getChargeItemId());
            ZhwyPaymentmethod zhwyPaymentmethod = zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zcd.getPayId());
            zcd.setShopsCode(zhwyShops.getCode());
            if (zhwyChargeitem != null) {
                zcd.setChargeItemName(zhwyChargeitem.getName());
            }
            if (zhwyPaymentmethod != null){
                zcd.setPaymentMethodName(zhwyPaymentmethod.getName());
            }
        }
        return list;
    }

    /**
     * 新增收费数据
     * 
     * @param zhwyChargedata 收费数据
     * @return 结果
     */
    @Override
    public int insertZhwyChargedata(ZhwyChargedata zhwyChargedata)
    {
        return zhwyChargedataMapper.insertZhwyChargedata(zhwyChargedata);
    }

    /**
     * 修改收费数据
     * 
     * @param zhwyChargedata 收费数据
     * @return 结果
     */
    @Override
    public int updateZhwyChargedata(ZhwyChargedata zhwyChargedata)
    {
        return zhwyChargedataMapper.updateZhwyChargedata(zhwyChargedata);
    }

    /**
     * 批量删除收费数据
     * 
     * @param ids 需要删除的收费数据主键
     * @return 结果
     */
    @Override
    public int deleteZhwyChargedataByIds(Long[] ids)
    {
        return zhwyChargedataMapper.deleteZhwyChargedataByIds(ids);
    }

    /**
     * 删除收费数据信息
     * 
     * @param id 收费数据主键
     * @return 结果
     */
    @Override
    public int deleteZhwyChargedataById(Long id)
    {
        return zhwyChargedataMapper.deleteZhwyChargedataById(id);
    }

    /**
     * 获取每月的缴纳金额 -- 首页图标展示
     * @param currentTime --当前日期
     * @param beforeSix -- 六个月前
     * @return -- 符合的数据集
     */
    @Override
    public List<ZhwyChargedata> queryZhwyDate(String currentTime, String beforeSix) {
        return zhwyChargedataMapper.queryZhwyDate(currentTime, beforeSix);
    }

    @Override
    public List<ZhwyChargedata> queryZhwyMonthData(String start, String end) {
        return zhwyChargedataMapper.queryZhwyMonthData(start,end);
    }

}
